package likou.tanxin;

/**
 * @Info:
 * @Author: 唐小尊
 * @Date: 2020/10/13 08:26
 */
public class _649 {
    public static void main(String[] args) {
        System.out.println(predictPartyVictory("RD"));
    }

    public static String predictPartyVictory(String senate) {
        int n = senate.length();
        int i = 0;
        while (true) {
            char now = senate.charAt(i);
            if (now == 'D') {
                int nextDi = (senate + senate).indexOf('R', i);
                if (nextDi == -1) {
                    return "Dire";
                } else {
                    nextDi %= senate.length();
                    senate = senate.substring(0, nextDi) + senate.substring(nextDi + 1, n);
                    if (nextDi <= i) {
                        i--;
                    }
                    n--;
                }
            } else {
                int nextDi = (senate + senate).indexOf('D', i);
                if (nextDi == -1) {
                    return "Radiant";
                } else {
                    nextDi %= senate.length();
                    senate = senate.substring(0, nextDi) + senate.substring(nextDi + 1, n);
                    if (nextDi <= i) {
                        i--;
                    }
                    n--;
                }
            }
            i++;
            if (i == n) {
                i = 0;
            }
        }
    }

    public String predictPartyVictory2(String senate) {

        boolean R = true, D = true;
        char[] arr = senate.toCharArray();

//      标记变量，当person > 0的时候，此时R方有权利把D方的成员淘汰掉
//      当person < 0的时候，D方有权利把R方的成员淘汰掉
        int person = 0;
        while (R && D) {

//        	当遇到双方成员的时候置为true，表示当前还有人员存在
//        	当R或D为false，表示一方已经没有成员存在了，胜负已分，结束循环
            R = false;
            D = false;

            for (int i = 0; i < arr.length; ++i) {
                if (arr[i] == 'R') {
                    R = true;
                    if (person < 0) {
                        arr[i] = 'O';
                    }
                    ++person;
                } else if (arr[i] == 'D') {
                    D = true;
                    if (person > 0) {
                        arr[i] = 'O';
                    }
                    --person;
                }
            }
        }
        return person > 0 ? "Radiant" : "Dire";
    }
}
